<?php

// +----------------------------------------------------------------------
// | 柠檬开发框架
// +----------------------------------------------------------------------
// | 零门槛、轻代码、响应式、清爽、简洁
// +----------------------------------------------------------------------
// | 本项目基于EasyAdmin进行二次开发
// +----------------------------------------------------------------------
// | 相关技术文档可参考：http://easyadmin.99php.cn/docs/index.html
// +----------------------------------------------------------------------


namespace app\admin\controller\lemon;


use app\admin\model\SystemNode;
use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\auth\Node as NodeService;
use think\App;

/**
 * Class Module
 * @package app\admin\controller\lemon
 * @ControllerAnnotation(title="柠檬助手-模块管理")
 */
class Module extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemNode();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $num = 1; //索引
            $appDirName = ROOT_PATH.DS.'app'.DS.'admin'.DS.'controller'; //后台应用路径
            $appDirHandle = opendir($appDirName); //用opendir打开目录
            //使用readdir循环读取目录里的内容
            while($moduleName = readdir($appDirHandle)){
                //将将目录下的文件和当前目录连接起来，才能在程序中使用
                $moduleDirName = $appDirName.DS.$moduleName; //模块路径
                $moduleDirType = filetype($moduleDirName);
                if($moduleDirType=='dir'){
                    if($moduleName != '.' && $moduleName != '..'){
                        $infoPath = $moduleDirName.DS.'info.json'; //模块信息配置文件
                        if(file_exists($infoPath)){
                            $infoJsonString = file_get_contents($infoPath);
                            // 用参数true把JSON字符串强制转成PHP数组 
                            $info = json_decode($infoJsonString, true);
                            $list[] = [
                                'id'    => $num,
                                'pid'   => 0,
                                'status'=> 1,
                                'title' => $info['title'],
                                'name'  => $moduleName,
                                'ps'    => $info['ps'],
                                'versions'=>$info['versions'],
                                'size'  => get_fileize(filesize($moduleDirName)),
                                'update_time'=>date("Y/m/d H:i",filemtime($moduleDirName))
                            ];
                            $pid = $num;
                            $num++;
                            $moduleDirHandle = opendir($moduleDirName); //用opendir打开控制器目录
                            while($controller = readdir($moduleDirHandle)){
                                $controllerName = $moduleDirName.DS.$controller; //控制器路径
                                $controllerType = filetype($controllerName);
                                if($controllerType=='file'&&pathinfo($controller, PATHINFO_EXTENSION)=='php'){
                                    $list[] = [
                                        'id'    => $num,
                                        'pid'   => $pid,
                                        'status'=> 1,
                                        'title' => $controller,
                                        'name'  => $moduleName.'.'.str_replace(strrchr($controller, "."),"",$controller),
                                        'ps'    => '',
                                        'versions'=>'',
                                        'size'  => get_fileize(filesize($controllerName)),
                                        'update_time'=>date("Y/m/d H:i",filemtime($controllerName))
                                    ];
                                    $num++;
                                }
                            }
                            closedir($moduleDirHandle); //关闭文件操作句柄
                        }
                    }
                }
            }
            closedir($appDirHandle); //关闭文件操作句柄
            if (input('selectFields')) {
                return $this->selectList();
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $num,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {

    }
}